import { Component, ViewChild, ElementRef } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { NFC, Ndef } from '@ionic-native/nfc';

declare var cordova;

@IonicPage()
@Component({
  selector: 'page-nfctest',
  templateUrl: 'nfctest.html'
})
export class NfcTestPage {

  global:any={NfcFlag:true,tagContent:''};
  IDCard:any={ Id:'',
   cartb:'',
   Name:'',
   Sex:'',
   Nation:'',
   Born:'',
   Address:'',
   IDCardNo:'',
   GrantDept:'',
   UserLifeBegin:'',
   UserLifeEnd:'',
   Photo:'',
   PhotoPathwlt :'',
   PhotoPathbmp:''  };




 /* public void Init(string id, string cartb, byte[] pucPHMsg)
{
  this.Id = id;
  this.cartb = cartb;
  this.Name = cartb.Substring(0, 15).Trim();//
  this.Sex = cartb.Substring(15, 1).Trim();//new string(cartb, 15, 1);
  this.Nation = cartb.Substring(16, 1).Trim();//new string(cartb, 16, 2);
  this.Born = cartb.Substring(18, 8).Trim();//new string(cartb, 18, 8);
  this.Address = cartb.Substring(26, 35).Trim();//(new string(cartb, 26, 35)).Trim();
  this.IDCardNo = cartb.Substring(61, 18).Trim();// new string(cartb, 61, 18);
  this.GrantDept = cartb.Substring(79, 15).Trim();//(new string(cartb, 79, 15)).Trim();
  this.UserLifeBegin = cartb.Substring(94, 8).Trim();//new string(cartb, 94, 8);
  this.UserLifeEnd = cartb.Substring(102, 8).Trim();//new string(cartb, 102, 8);
  this.Photo = pucPHMsg;
  PhotoPathwlt = this.IDCardNo + this.Id + ".wlt";
  PhotoPathbmp = this.IDCardNo + this.Id + ".bmp";
}*/

  constructor(private nfc: NFC, private ndef: Ndef ) {
    console.error("============================初始化NFC页面") ;

  }

  ionViewWillLeave() {
    console.error("============================即将离开NFC页面");
    this.global.NfcFlag = false;
    //document.removeEventListener("ndef-mime", this.nfcListenerSubscribe, false);
    //document.removeEventListener("tag", this.nfcListenerSubscribe, false);
  }

  ionViewDidEnter() {
    this.global.NfcFlag = true; //允许NFC
    console.error("============================在进入页面时重置flag为true，又可以处理NFC事件了！");
    //document.addEventListener("ndef-mime", this.nfcListenerSubscribe.bind(this), false);
    //document.addEventListener("tag", this.nfcListenerSubscribe.bind(this), false);
  }

  nfcListenerSubscribe(event){
    console.error("============================识别到的NFC标记为:" + JSON.stringify(event));

    /* if (!this.global.NfcFlag) {
       console.error("请等待上一次处理结束");
       return false;
     }*/
    this.global.NfcFlag = false;
    if (event && event.tag && event.tag.id && event.tag.ndefMessage && event.tag.ndefMessage.length) {
      let payload = event.tag.ndefMessage[0]["payload"];
      //标记语言字段的长度
      let langLength = payload[0] & 63;
      //跳过语言标记
      let tagContent = this.nfc.bytesToString(payload).slice(langLength + 1);
      this.global.tagContent=tagContent;
      console.error('============================成功扫描到NFC标签内容：' + tagContent);
      /*if (tagContent) {
        //拿到tagContent，解析id
        let ucode = this.getCode(tagContent, 'ucode');
        if (ucode) {
          //解析到正确的ucode,开始鉴权
          this.checkLegality(ucode);
        } else {
          //解析失败
          this.errorUcode(tagContent);
        }
      } else {
        this.errorUcode(tagContent);
      }*/
    } else {
      //无法识别的tag
      console.error("============================法识别的tag"  );
      //this.errorUcode("");
    }


    //let message = this.ndef.textRecord('Hello world');
    //this.nfc.share([message]).then(onSuccess).catch(onError);
  }

  addListenerSuccess(){
    console.error('============================successfully attached ndef listener');
  }

  addListenerError(err){
    console.error('============================error attaching ndef listener', err);
  }

  addNdefListener() {

   /* if (typeof(this.nfc) == "undefined") {
      console.error('============================您的机器没有NFC功能,或者NFC功能没有打开');
    } else {
      //旧系统使用监听
      //this.nfc.addTagDiscoveredListener(this.addListenerSuccess, this.addListenerError).subscribe(this.nfcListenerSubscribe);
      //新系统使用监听
      //this.nfc.addNdefFormatableListener(this.addListenerSuccess, this.addListenerError).subscribe(this.nfcListenerSubscribe);
      this.nfc.addNdefListener(this.addListenerSuccess, this.addListenerError).subscribe(this.nfcListenerSubscribe);
    }*/


    var _params = {};
    _params['Longitude'] = 'ccc';
    console.log("------------------------------callSomething：" + JSON.stringify(_params));
    var extraInfo = cordova.require('com.onluy.plugins.nfcidcard.nfcidcard');
    extraInfo.SHOW_SETTINGS(  _params,
      function (message) {   },
      function (message) { alert("b"+message); });

  }


  addbeginSession() {
    console.error('============================addbeginSession');
    this.nfc.beginSession(this.addListenerSuccess, this.addListenerError).subscribe(this.nfcListenerSubscribe);
  }





}
